#ifndef __TERRAIN
#define __TERRAIN

#include "maya.h"
#include "evector.h"
#include "per.hpp"

class Terrain
{
protected:
	QVector<double> roche;
	QVector<double> sable;
	QVector<double> eau;
	QVector<double> sedimentEau;
	Vector o;
	double l;
	int n;
public:
	Terrain();
	void generate();

	void arrachement();
	void transport();
	void erosion(int k=1);

	void poseEau(double h);
	void dissolution();
	void deplacementEau();
	void depot();
	void evaporation();
	void erosionHydrolique(bool);

	MayaGeometry createMesh();

	inline Vector Q(int x, int y)
	{
		Vector res(x*l,y*l,0);
		res = res+o;
		return res;
	}
	inline Vector P(int x, int y)
	{
		Vector res(x*l,y*l,roche[y*n+x]+sable[y*n+x]+eau[y*n+x]+sedimentEau[y*n+x]);
		res = res+o;
		return res;
	}
	inline Vector vec(double x,double y)
	{
		Vector v1,v2,v3,v4;
		double z1,z2,z3;
		double i,j;
		int m1,m2;
		m1=(int)(x/l);
		m2=(int)(y/l);
		i=x/l-(double)m1;
		j=y/l-(double)m2;
		v1=P(m1,m2);
		v2=P(m1+1,m2);
		v3=P(m1,m2+1);
		v4=P(m1+1,m2+1);
		z1=v1[2]-i*(v1[2]-v2[2]);
		z2=v3[2]-i*(v3[2]-v4[2]);
		z3=z1-j*(z1-z2);
		Vector res(x,y,z3);
		res=res+o;
		return res;
	}
	inline double getSable(double x,double y)
	{
		double z1,z2,z3;
		double i,j;
		int m1,m2;
		m1=(int)(x/l);
		m2=(int)(y/l);
		i=x/l-(double)m1;
		j=y/l-(double)m2;
		z1=sable[m2*n+m1]-i*(sable[m2*n+m1]-sable[m2*n+m1+1]);
		z2=sable[(m2+1)*n+m1]-i*(sable[(m2+1)*n+m1]-sable[(m2+1)*n+m1+1]);
		z3=z1-j*(z1-z2);
		return z3;
	}
};

#endif